package aod.android.com.bernay.mine.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.loginandregister.bean.UserDataBean;
import aod.android.com.bernay.util.ToastUtil;
import aod.android.com.bernay.util.ValidatorUtil;
import aod.android.com.bernay.view.activity.MainActivity;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/4 10:01
 * @Email jamesKing0812@gmail.com
 */
public class SetNewPasswordActivity extends BernayBaseActivity {

    @HiView(R.id.et_newPassword)
    EditText mNewPassword;

    @HiView(R.id.et_passwordAgain)
    EditText mPasswordAgain;

    private String newPassword;
    private String confirmPassword;
    private String msgCode;
    private String loginName;

    @Override
    protected int initLayout() {
        return R.layout.activity_set_new_password;
    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        msgCode = extras.getString("msgCode");
        loginName = extras.getString("loginName");

        initTitle(getString(R.string.setting_new_password), getString(R.string.determine), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newPassword = mNewPassword.getText().toString();
                confirmPassword = mPasswordAgain.getText().toString();
                if (checkPasswordRule(newPassword,confirmPassword)){
                    retrievePassword();
                }
            }
        });
    }

    /**
     * 校验密码规则
     * @param password1
     * @param password2
     * @return
     */
    public boolean checkPasswordRule(String password1,String password2){
        if ("".equals(password1)){
            showToast("新密码不能为空");
            return false;
        }else if("".equals(password2)){
            showToast("确认密码不能为空");
            return false;
        }else if(!password1.equals(password2)){
            showToast(getString(R.string.password_input));
            return false;
        }else if(!ValidatorUtil.isPassword(password1)){
            showToast("新密码密码为数字字母");
            return false;
        }else if (!ValidatorUtil.isPassword(password2)){
            showToast("确认密码密码为数字字母");
            return false;
        }else{
            return true;
        }
    }

    /**
     * 修改密码
     */
    public void retrievePassword(){
        BernayApi.http().resetpass(loginName, msgCode, newPassword, confirmPassword, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                UserDataBean userDataBean =  gson.fromJson(json,UserDataBean.class);
                if(userDataBean.getStatus()==1){
                    goActivity(SettingActivity.class);
                    showToast("密码修改成功");
                    finish();
                }else if(userDataBean.getStatus()==-1){
                    showToast("密码修改失败");
                }else {
                    showToast("密码修改失败");
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                LogUtils.json(new Gson().toJson(e));
            }
        });
    }
}
